/*
 * @Author: ChunLai
 * @Date: 2022-10-09 09:35:51
 * @LastEditTime: 2025-09-08 14:17:08
 * @Description: 银行卡更换
 * @FilePath: \02.bldinsure\src\views\bankcard\alter.js
 */
import { mapGetters } from "vuex";
import { tellPhoneFormat, accountCodeFormat } from "@/methods/format.js";
import { bankCardAttribution } from "@/utils/GetBankName";
export const bankCardAlterMixin = {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      orderInfo: "insert/payInfo",
      bankR: "dictionary/bankR",
      bank7: "dictionary/bank7",
      bankPhoto: "page/bankPhoto",
      payModeList: "dictionary/payModeList",
      inputReg: "infoReg/inputReg",
      dontChangePayMode: "page/dontChangePayMode",
      bankTransferAuthorization: "dictionary/bankTransferAuthorization",
    }),
    infoReg() {
      return this.inputReg.payInfo;
    },
    viewOptions() {
      return this.popConfig.list.filter(
        (item) => item.text.indexOf(this.keyWord) !== -1
      );
    },
    showSignFile() {
      let flag = false;
      if (this.bankTransferAuthorization.btnText) {
        flag = true;
      }
      return flag;
    },
    cpPhone() {
      return function (value) {
        return tellPhoneFormat(value);
      };
    },
    cpAccountNo() {
      return function (value) {
        return accountCodeFormat(value);
      };
    },
    bankList() {
      let res = [];
      if (this.cardInfo.payMode == "R") {
        res = this.bankR;
      } else {
        res = this.bank7;
      }
      return res;
    },
    payModeLabel() {
      let res = "";
      if (this.cardInfo.payMode !== void 0) {
        const obj = this.payModeList.find(
          (ele) => ele.value == this.cardInfo.payMode
        );
        if (obj) {
          res = obj.label;
        }
      }
      return res;
    },
    limitMoney() {
      let res = "";
      if (this.cardInfo.bankCode) {
        const obj = this.bankList.find(
          (ele) => ele.keyId === this.cardInfo.bankCode
        );
        if (obj) {
          res = this.cardInfo.payMode == "R" ? obj.small : obj.big;
        }
      }
      return res;
    },
  },
  methods: {
    showPopAction(type, key, list) {
      if (list.length == 1) {
        return;
      }
      this.popConfig = {
        type,
        key,
        list,
      };
      // console.log(this.popConfig);
      this.showPop = true;
    },
    addresssConfirm(value) {
      // console.log(value)
      if (this.popConfig.key == "bankArea") {
        this.changeCardData({
          rgtAddress: value.rgtAddress, // 省市
          bankArea: value.addressDetail, // 省市区
          openBankCity: value.cityCode,
          openBankCounty: value.countyCode,
          openBankProvince: value.provinceCode,
          openBankProvinceName: value.provinceName,
        });
      }
      this.showPop = false;
    },
    changeCardData(data) {
      Object.assign(this.cardInfo, data);
      this.$forceUpdate();
    },
    changeData(data) {
      Object.assign(this.orderInfo, data);
    },
    // 上传图片
    afterRead(file) {
      if (file.file.size > 30720) {
        // 大于30KB的图片--压缩上传
        let canvas = document.createElement("canvas");
        let context = canvas.getContext("2d");
        let img = new Image();
        img.src = file.content;
        img.onload = () => {
          const whRatio = img.width / img.height;
          const nw = 500;
          const nh = Math.ceil(nw / whRatio);
          canvas.width = nw;
          canvas.height = nh;
          context.drawImage(img, 0, 0, nw, nh);
          file.content = canvas.toDataURL(file.file.type, 0.92);
          this.getFileUpload(file.content);
        };
      } else {
        //小于1M直接上传
        this.getFileUpload(file.content);
      }
    },
    // 上传图片
    getFileUpload(imgBase) {
      this.$store.dispatch("apis/fileAdminUpload", imgBase).then((res) => {
        if (res.result == "1") {
          this.ocrCheckCommBefore(res.url);
        } else {
          this.$toast(res.message);
        }
      });
    },
    ocrCheckCommBefore(picUrl) {
      // 只有身份证和银行卡需要
      this.$store.commit("updateLoad", true);
      this.$store.commit("updateLoadTxt", "证件识别中");
      this.$http
        .post("/aliyun/oss/ocrBankCardRecoganize", {
          url: picUrl,
        })
        .then(({ data }) => {
          this.$store.commit("updateLoadTxt", "信息比对中");
          let resMsg = data.data;
          this.bankUrl = picUrl;
          this.getBankCode(resMsg.bankName, resMsg.cardNumber);
        })
        .catch(() => {
          this.$store.commit("updateLoad", false);
          this.$baseConfirm(
            "未能识别出银行卡信息，如继续上传，会导致问题件产生。是否继续上传?",
            () => {
              this.bankUrl = picUrl;
            },
            null,
            "继续上传",
            "重新上传"
          );
        });
    },
    getBankCode(bankName, bankNo) {
      this.cardInfo.bankAccNo = bankNo;
      this.cardInfo.bankName = bankName;
      let value = bankName;
      // 通过银行卡号获取银行卡名称（对应字典名称）
      let info = bankCardAttribution(bankNo);
      if (info && info.bankName) {
        value = info.bankName;
      }
      let target = this.bankList.find((item) => {
        if (
          item.text == value ||
          value.indexOf(item.text) !== -1 ||
          item.text.indexOf(value) !== -1
        ) {
          this.cardInfo.bankCode = item.keyId;
          return true;
        }
        return false;
      });
      this.$store.commit("updateLoad", false);
      if (!target) {
        this.$toast("当前产品投保不支持该银行卡,请更换银行卡或手动输入");
        this.cardInfo.bankCode = "";
        this.cardInfo.bankName = "";
      } else {
        if (target.bank7 && target.bankR) {
          //
          this.cardInfo.payMode = "R";
        } else if (!target.bank7 && target.bankR) {
          // 仅支持实时扣款
          this.cardInfo.payMode = "R";
        } else if (!target.bankR && target.bank7) {
          // 仅支持批次
          this.cardInfo.payMode = "7";
        }
      }
    },
  },
};
